/** 
 * Algorithm.java
 * @author Lex
 * 2007-9-22 ����08:27:02
 */
package org.lex.checksum.core;

import java.security.Security;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class AlgorithmService {
	public static final String CHECKSUM_ADLER32 = "Adler32";
	public static final String CHECKSUM_CRC32 = "CRC32";

	private static final AlgorithmService INSTANCE = new AlgorithmService();

	public static AlgorithmService getInstance() {
		return INSTANCE;
	}

	public Collection<String> getAlogrithms() {
		return Collections.unmodifiableCollection(this.algorithmMap.keySet());
	}

	public Algorithm getAlgorithm(String name) {
		return this.algorithmMap.get(name).clone();
	}

	private Map<String, Algorithm> algorithmMap = null;

	private AlgorithmService() {
		this.algorithmMap = new TreeMap<String, Algorithm>();
		// init Checksum Algorithms
		this.registerChecksum(CHECKSUM_ADLER32);
		this.registerChecksum(CHECKSUM_CRC32);
		// init MessageDigest Algorithms
		for (String algo : Security.getAlgorithms("MessageDigest")) {
			this.algorithmMap.put(algo, new MessageDigestAlgorithm(algo));
		}
	}

	private void registerChecksum(String name) {
		algorithmMap.put(name, new ChecksumAlgorithm(name));
	}
}
